//
//  ByViewShopCarToolBar.m
//  oc_knowledge
//
//  Created by pro on 17/1/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewShopCarToolBar.h"

@implementation ByViewShopCarToolBar
{
    // notes
    CGFloat _minLeft;

    
    // view
    UIView *_normorView;        // 正常视图
    UIView *_editingView;       // 编辑中视图
}
- (void)dealloc {
    
    [self removeObserver:self forKeyPath:@"editing"];
    [self removeObserver:self forKeyPath:@"selectAll"];
    [self removeObserver:self forKeyPath:@"totalPrice"];
    [self removeObserver:self forKeyPath:@"selectCount"];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self setCommonUI];
        [self setupNormalUI];
        [self setupEditingUI];
        // 添加KVO
        [self addObserver:self forKeyPath:@"editing" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"selectAll" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"totalPrice" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"selectCount" options:NSKeyValueObservingOptionNew context:nil];

    }
    return self;
}

#pragma mark - KVO事件通知方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"editing"]) {
        
        BOOL isEditing = [change[NSKeyValueChangeNewKey] boolValue];
        _normorView.hidden = isEditing;
        _editingView.hidden = !isEditing;
    }
    
    if ([keyPath isEqualToString:@"selectAll"]) {
        
        BOOL isSelectAll = [change[NSKeyValueChangeNewKey] boolValue];
        UIButton *selectBtn = [self viewWithTag:111];
        selectBtn.selected = isSelectAll;
    }
    if ([keyPath isEqualToString:@"totalPrice"]) {
        
        CGFloat totalPrice = [change[NSKeyValueChangeNewKey] floatValue];
        UILabel *totalPriceLabel = [_normorView viewWithTag:222];
        totalPriceLabel.text = [NSString stringWithFormat:@"合计：￥%.2lf", totalPrice];
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:totalPriceLabel.text];
        NSRange maohaoRange = [totalPriceLabel.text rangeOfString:@"："];
        NSRange priceRange = NSMakeRange(maohaoRange.location + 1, totalPriceLabel.text.length - maohaoRange.location - 1);
        NSRange integeRange = NSMakeRange(priceRange.location + 1, priceRange.length - 4);
        [attr addAttribute:NSForegroundColorAttributeName value:KShopCarThemeColor range:priceRange];
        [attr addAttribute:NSFontAttributeName value:FONT(20) range:integeRange];
        
        totalPriceLabel.attributedText = attr;
    }
    if ([keyPath isEqualToString:@"selectCount"]) {
        
        int selectCount = [change[NSKeyValueChangeNewKey] intValue];
        UIButton *settleBtn = [_normorView viewWithTag:166];
        
        [settleBtn setTitle:[NSString stringWithFormat:@"结算(%i)", selectCount] forState:UIControlStateNormal];

    }
}

#pragma mark - 创建UI
- (void)setCommonUI {
    
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.5)];
    topLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:topLine];
    
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn.frame = CGRectMake(0, 0,self.height, self.height);
    [selectBtn setImage:[UIImage imageNamed:@"Common_unSelected"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"Common_selected"] forState:UIControlStateSelected];
    CGFloat margin = (selectBtn.width - 20) / 2;
    [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(margin, margin, margin, margin)];
    [selectBtn addTarget:self action:@selector(selectAll:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:selectBtn];
    selectBtn.tag = 111;
    
    
    NSString *text = @"全选";
    CGFloat fontSize = 16;
    CGFloat width = [text getSizeOfString:fontSize].width + 10;
    UILabel *selectAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(selectBtn.right, 0, width, self.height)];
    selectAllLabel.font = FONT(fontSize);
    selectAllLabel.text = text;
    selectAllLabel.textColor = [UIColor blackColor];
    selectAllLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:selectAllLabel];
    
    
    // 记录右侧视图的最大范围
    _minLeft = selectAllLabel.right;

}

- (void)setupNormalUI {
    
    _normorView = [[UIView alloc] initWithFrame:CGRectMake(_minLeft, 0, self.width - _minLeft, self.height)];
    [self addSubview:_normorView];
    
    // 结算按钮
    UIButton *settleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    settleBtn.frame = CGRectMake(0, 0, _normorView.width / 3, _normorView.height);
    settleBtn.right = _normorView.width;
    settleBtn.backgroundColor = KShopCarThemeColor;
    settleBtn.titleLabel.font = FONT(15);
    [settleBtn setTitle:@"结算(0)" forState:UIControlStateNormal];
    [settleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [settleBtn addTarget:self action:@selector(settle) forControlEvents:UIControlEventTouchUpInside];
    [_normorView addSubview:settleBtn];
    settleBtn.tag = 166;
    
    // 合计label
    UILabel *totalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _normorView.width - settleBtn.width, _normorView.height)];
    totalPriceLabel.adjustsFontSizeToFitWidth = YES;
    totalPriceLabel.font = FONT(14);
    totalPriceLabel.text = @"合计：￥0.0";
    totalPriceLabel.textColor = [UIColor blackColor];
    totalPriceLabel.textAlignment = NSTextAlignmentCenter;
    [_normorView addSubview:totalPriceLabel];
    totalPriceLabel.tag = 222;
    
    
    self.totalPrice = 0.00;
}

- (void)setupEditingUI {
    
    _editingView = [[UIView alloc] initWithFrame:CGRectMake(_minLeft, 0, self.width - _minLeft, self.height)];
    [self addSubview:_editingView];
    _editingView.hidden = YES;
    
    // 删除按钮
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, _editingView.width / 3, _editingView.height);
    deleteBtn.right = _editingView.width;
    deleteBtn.backgroundColor = KShopCarThemeColor;
    deleteBtn.titleLabel.font = FONT(15);
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [_editingView addSubview:deleteBtn];
}

#pragma mark - 点击事件
// 全选
- (void)selectAll:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    
    if (self.selectAllBlock) {
        self.selectAllBlock(sender.selected);
    }
}
// 删除
- (void)delete {
    
    if (self.deleteAllBlock) {
        self.deleteAllBlock();
    }
}
// 结算
- (void)settle {
    
    if (self.settleBlock) {
        self.settleBlock();
    }
}


@end
